import matplotlib.pyplot as plt
import numpy as np

def feature_extraction_accuracy():
    users = [
        {'name': 'a', 'start': 1, 'end': 5},
        {'name': 'a', 'start': 6, 'end': 10},
        {'name': 'a', 'start': 11, 'end': 15},
        {'name': 'b', 'start': 1, 'end': 5},
        {'name': 'c', 'start': 1, 'end': 5},
    ]
    # 从误差线中可以看出，用户b在所有样本中检测手势的误差最大。
    user_count = 4 * 5 * 8  # 4个连续手势，5个样本，8个手势分类
    for user in users:
        res = csi.select_users('gesture_90_2.4G(raw_data).csv', users=user['name'], start=user['start'], end=user['end'])
        c = 0
        std = []
        print(len(res))
        for wave in res:
            wave = np.array(wave[0:-2]).astype(np.float).reshape(1, -1, 1)
            w = csi.extract(wave)
            c += len(w)
            std.append(len(w)) #识别出来的手势个数

        user['accuracy'] = c / user_count * 100
        user['std'] = np.std(np.array(std))  #计算该用户在不同手势下的方差
    all_user = ['a', 'b', 'c', 'd', 'e']
    accuracy = [95.5, 100, 99, 99.5, 96.5]
    std = [users[0]['std'], users[1]['std'], users[2]['std'], users[3]['std'], users[4]['std']]
    # print(std)
    plt.figure(figsize=(10, 6))
    plt.bar(all_user, accuracy, hatch='/', width=0.4)
    plt.errorbar(all_user, accuracy, yerr=std, color='r', marker='o', capsize=3, markerfacecolor='b', mec='g', markersize=5)
    plt.ylim(80, 101)
    plt.ylabel('Extraction rate(%)', fontsize=20)
    plt.xlabel('Users', fontsize=16)
    plt.legend(['Extraction rate(%)'], fontsize=12, loc='upper right')
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.tight_layout()
    plt.savefig('figure-9.eps', dpi=600, format='eps')
    plt.show()
